package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.AssistEntList;
import com.cz.nongyetongv5.bean.AssistEntListItemBean;
import com.cz.nongyetongv5.bean.AssistEntListResponse;
import com.cz.nongyetongv5.bean.EntDetailBean;
import com.cz.nongyetongv5.bean.JsonUtil;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class AssistEntUserListActivity extends Activity implements View.OnClickListener {

    private RelativeLayout btnBack;
    RecyclerView recyclerView;
    private String region_code;
    private String year;
    private int type;
    private TextView tv_title;
    private String lacationTag;
    private ListAdapter adapter;
    SmartRefreshLayout refreshLayout;
    private int page = 1;
    private int size = 10;
    private AssistEntListResponse assistEntListResponse;
    private AssistEntListItemBean assistEntListItemBean;
    private List<AssistEntList> assistEntLists;
    private int isSame;
    private String add_user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assist_ent_list);
        assistEntLists = new ArrayList<>();
        btnBack = findViewById(R.id.btnBack);
        tv_title = findViewById(R.id.tv_title);
        btnBack.setOnClickListener(this);
        recyclerView = findViewById(R.id.recyclerView);
        refreshLayout = findViewById(R.id.refreshLayout);
        region_code = getIntent().getStringExtra("region_code");
        year = getIntent().getStringExtra("year");
        type = getIntent().getIntExtra("type", 0);
        add_user = getIntent().getStringExtra("add_user");
        isSame = getIntent().getIntExtra("isSame", 0);
        if (type == 1) {
            tv_title.setText("新增主体明细");
            lacationTag = "1";
            getListType();
        } else if (type == 2) {
            tv_title.setText("更新主体明细");
            lacationTag = "2,4";
            getListType();
        } else if (type == 3) {
            tv_title.setText("注销主体明细");
            lacationTag = "3";
            getListType();
        }
        adapter = new ListAdapter();
        View notDataView = getLayoutInflater().inflate(R.layout.view_nodata, (ViewGroup) recyclerView.getParent(), false);
        adapter.setEmptyView(notDataView);
        recyclerView.setLayoutManager(new LinearLayoutManager(AssistEntUserListActivity.this));
        recyclerView.setAdapter(adapter);
        refreshLayout.setDisableContentWhenRefresh(true);
        refreshLayout.setDisableContentWhenLoading(true);
        refreshLayout.setRefreshHeader(new ClassicsHeader(AssistEntUserListActivity.this));
        //设置 Footer
        refreshLayout.setRefreshFooter(new ClassicsFooter(AssistEntUserListActivity.this));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;

                getListType();

                //                WaitDialog.ShowDialog(getActivity(),"数据加载中...");
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (assistEntListItemBean != null) {
                    if (page < assistEntListItemBean.getTotal()) {
                        page++;
                        getListType();
                    } else {
                        refreshLayout.finishLoadmore(200);
                        Toast.makeText(AssistEntUserListActivity.this, "没有更多数据", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    refreshLayout.finishLoadmore(200);
                    Toast.makeText(AssistEntUserListActivity.this, "没有数据", Toast.LENGTH_SHORT).show();
                }
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    getDetailType(assistEntLists.get(position).getE_id());
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnBack:
                finish();
                break;
            default:
                break;
        }
    }


    void getList() {
        OkHttpUtils
                .get()
                .url(InitApplication.newUrl + "ent/allKindEntList.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("region_code", region_code)
                .addParams("page", String.valueOf(page))
                .addParams("size", String.valueOf(size))
                .addParams("add_user", add_user)
                .addParams("isSame", String.valueOf(isSame))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(AssistEntUserListActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        System.out.println(response);
                        closeRefresh();
                        assistEntListResponse = JsonUtil.toObject(response, AssistEntListResponse.class);
                        if (assistEntListResponse.getCode() == 0) {
                            assistEntListItemBean = assistEntListResponse.getData();
                            if (page == 1) {
                                assistEntLists.clear();
                                assistEntLists.addAll(assistEntListItemBean.getRows());
                            } else {
                                assistEntLists.addAll(assistEntListItemBean.getRows());
                            }
                            adapter.notifyDataSetChanged();
                        } else {
                            Toast.makeText(AssistEntUserListActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }


    /**
     * id
     *
     * @param id
     */

    void getDetail(String id) {
        OkHttpUtils
                .get()
                .url(InitApplication.newUrl + "ent/entDetail.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("id", id)
                .build()

                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(AssistEntUserListActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        System.out.println(response);
                        Gson gson = new Gson();
                        EntDetailBean entDetailBean = gson.fromJson(response, EntDetailBean.class);
//                        EntDetailBean entDetailBean = JsonUtil.toObject(response, EntDetailBean.class);
                        if (entDetailBean.getCode() == 0) {
                            if (entDetailBean.getData() != null) {
                                Intent intent = new Intent(AssistEntUserListActivity.this, EntDetailActivity.class);
                                intent.putExtra("add_user", "");
                                intent.putExtra("region_code", region_code);
                                intent.putExtra("type", 2);
                                intent.putExtra("entBean", entDetailBean.getData());
                                startActivity(intent);
                            } else {
                                Toast.makeText(AssistEntUserListActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(AssistEntUserListActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }


    void getListType() {
        OkHttpUtils
                .get()
                .url(InitApplication.newUrl + "ent/allKindAssistEntList.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("region_code", region_code)
                .addParams("page", String.valueOf(page))
                .addParams("size", String.valueOf(size))
                .addParams("add_user", add_user)
                .addParams("isSame", String.valueOf(isSame))
                .addParams("year", year)
                .addParams("kind", "0")
                .addParams("lacationTag", lacationTag)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(AssistEntUserListActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        System.out.println(response);
                        closeRefresh();
                        assistEntListResponse = JsonUtil.toObject(response, AssistEntListResponse.class);
                        if (assistEntListResponse.getCode() == 0) {
                            assistEntListItemBean = assistEntListResponse.getData();
                            if (page == 1) {
                                assistEntLists.clear();
                                assistEntLists.addAll(assistEntListItemBean.getRows());
                            } else {
                                assistEntLists.addAll(assistEntListItemBean.getRows());
                            }
                            adapter.notifyDataSetChanged();
                        } else {
                            Toast.makeText(AssistEntUserListActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    /**
     * e_id
     */
    void getDetailType(String e_id) {
        OkHttpUtils
                .get()
                .url(InitApplication.newUrl + "ent/detail.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("region_code", region_code)
                .addParams("id", e_id)
                .addParams("isSame", String.valueOf(isSame))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(AssistEntUserListActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        System.out.println(response);
                        Gson gson = new Gson();
                        EntDetailBean entDetailBean = gson.fromJson(response, EntDetailBean.class);
//                        EntDetailBean entDetailBean = JsonUtil.toObject(response, EntDetailBean.class);
                        if (entDetailBean.getCode() == 0) {
                            if (entDetailBean.getData() != null) {
                                Intent intent = new Intent(AssistEntUserListActivity.this, EntDetailActivity.class);
                                intent.putExtra("add_user", "");
                                intent.putExtra("region_code", region_code);
                                intent.putExtra("type", 2);
                                intent.putExtra("entBean", entDetailBean.getData());
                                startActivity(intent);
                            } else {
                                Toast.makeText(AssistEntUserListActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(AssistEntUserListActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }


    public class ListAdapter extends BaseQuickAdapter<AssistEntList, BaseViewHolder> {
        public ListAdapter() {
            super(R.layout.assist_ent_list_item, assistEntLists);
        }

        @Override
        protected void convert(BaseViewHolder helper, AssistEntList item) {
            helper.setText(R.id.tv_ent_name, item.getName());
            helper.setText(R.id.tv_address, item.getAddress());
            if (item.getKind().equals("7")) {
                if (item.getCharge() == null) {
                    helper.setText(R.id.tv_charge, item.getName());
                } else {
                    helper.setText(R.id.tv_charge, item.getCharge());
                }
            } else {
                helper.setText(R.id.tv_charge, item.getCharge());
            }
        }
    }

    public void closeRefresh() {
        if (refreshLayout != null) {
            if (refreshLayout.isRefreshing()) {
                refreshLayout.finishRefresh(200);
            } else if (refreshLayout.isLoading()) {
                refreshLayout.finishLoadmore(200);
            }
        }
    }
}
